﻿//我的笔记的js
var myScroll;
var value="";
var m=0;
var Count=0;
require.config({
    baseUrl: '/js/common', 
    paths: {
        libs: '/js/libs'
    }
});
define(function(require){
	// 加载js
	var rs_zepto = require('libs/zepto');
	var rs_yingSoftCookie = require('yingSoftCookie');
	var rs_yingSoftPrg = require('yingSoftPrg');
	var rs_agentInfo = require('agentInfo');
	var rs_ajax = require('ajax');
	var rs_md5 = require('md5');
	var rs_dialog = require('dialog');
	var rs_storage = require('storage');
	var rs_yingSoftSetTest =require('yingSoftSetTest');
	// 初始化
	var _isClick = true;	// 防止多次点击
	value=1;
	$('#wrapper').css('height',window.innerHeight-$('header').height())
	if(G_Prg.getQueryString('redoSwitch')==null){
		myProblem(0);
		G_Prg.popMsg('您当前处于做题模式');
	}else if(G_Prg.getQueryString('redoSwitch')=='0'){
		$("#tab_problem").attr('data-value','2');
		myProblem(0);
	}else if(G_Prg.getQueryString('redoSwitch')=='1'){
		$("#tab_problem").attr('data-value','2');
		myProblemRanking(0);
	}
	beiti();//切换
});
function myProblem(m) {
	//我的收藏
	var appID = G_Cookie.getAppID();
	var getGuid=G_Cookie.getGuid();
    var appEName = G_Cookie.getAppEName();
    var UserID=G_Cookie.getUserID();
    var getUserName=G_Cookie.getUserName();
    $('#loadingDiv').css('display','block');
    var _success = function (json) {
    	$('#pullUp').css('display','none');
    	var jsonData = JSON.parse(json);
    	if(jsonData.status==201){
    		
    	}else{
    		if(jsonData.status==200){
    			if(jsonData.data.count==0){
					$('.noNum').css('display','block');
					$('.noNum').text('您还没有笔记')
				}else{
					var info=jsonData.data.info;
		    		Count=jsonData.data.count;
		    		if(Count>50){
		    			$('#pullUp').css('display','block');
		    		}
					var datareplace=G_SetTestInfo.replaceImgURL(jsonData.data.test)
    				var data=datareplace.StyleItems;
		    		for(var i=0; i<data.length;i++){
		    			var infos=jsonData.data.info[i];
		    			var title='';
		    			var answer='';
		    			var list=data[i].TestItems[0];
		    			var explain='';
		    			if(data[i].Type=='A3TEST'){
		    				var titleF=list.A3TestItems[0];
		    				title=list.FrontTitle+titleF.Title;
		    				var Answers=titleF.Answer.split('');
							for(var j=0;j<titleF.SelectedItems.length;j++){
								for(var s=0;s<Answers.length;s++){
									if(Answers[s]==titleF.SelectedItems[j].ItemName){
										answer+=titleF.SelectedItems[j].Content+';';
									}
								}
		    				}
							explain=titleF.Explain;
		    			}else if(data[i].Type=='ATEST'||data[i].Type=='BTEST'||data[i].Type=='XTEST'){
		    				title=list.Title;
		    				answer='';
		    				var Answers=list.Answer.split('');
							for(var j=0;j<list.SelectedItems.length;j++){
								for(var s=0;s<Answers.length;s++){
									if(Answers[s]==list.SelectedItems[j].ItemName){
										answer+=list.SelectedItems[j].Content+';';
									}
								}
		    				}
							explain=list.Explain;
		    			}else{
		    				title=list.Title;
		    				answer=list.Answer;
							explain=list.Explain;
		    			}
		    			strLi = "<li allTestID='"+infos.AllTestID+"' childTableID='"+infos.ChildTableID+"' childTableName='"+data[i].Type+"' cptID='"+infos.CptID+"' srcID='"+infos.SrcID+"' SbjID='"+infos.SbjID+"' StyleID='"+infos.StyleID+"' page='"+m+"'>" 
						+ "<div class='problme_title' data-display='none' data-flase='true' ><span id=''>"+(i+1)+".</span><span class='detail_problem'>"+title+"</span></div>"
						+ "<div class='problme_key' style='display:block'>"
						+ "<div class='jie_da ans'  style='display:none'><span>答</span>"
						+ "<p>"+answer+"</p>" 
						+ "</div>" 
						+ "<div class='jie_da jie2'><span style='background-color:#FF6B6B'>笔</span>"
						+ "<p>"+infos.UserNote+"</p>" 
						+ "</div>" 
						+ "</div>" 
						+ "</li>";
						$("#list_problem").append(strLi);
						if($('#tab_problem').attr('data-value')==2){
							$('.problme_key .ans').remove();
						}
		    		}
		    		myscroll.refresh();
			 		display_none();
			 		tiaozhuan();
				}
    		}
    		
    		
    	}
    	for(var i=0;i<50;i++){
			$('.jie2').each(function(){
				if($(this).find('p').text()==0||$(this).find('p').text()=='null'){
					$(this).remove();
				}
			})
		}
    }
    var URL = '/api/exam/getUserNoteTest';
    G_AjaxApi_Record.post(URL,{
    	appID:appID,
//	     userID:UserID,
	     curPage:m,
	     guid:getGuid,
	     cptID:-1,
	     eachPageNum:50,
	     queryTestInfo:0,
	     queryKnoledge:0
    }, true, _success);
}
//背题模式和答题模式的切换
function beiti(){
	$("#tab_problem").on("click",function(){
		m=0;
		$('#list_problem').empty();
			var value=$(this).attr("data-value");
		if($(this).attr("data-value")=="1"){				
			$(this).text("背题模式");					
			$(this).attr("data-value","2");
			G_Prg.popMsg('您当前处于做题模式');
			myProblem(m);
		}else{				
			$(this).text("做题模式");
			$(this).attr("data-value","1");
			G_Prg.popMsg('您当前处于背题模式');
			myProblem(m);
		}
	})
}

//点击标题显示隐藏 的解析
function display_none() {
	$("#list_problem li").on("click", function() {
		if ($(this).find('.problme_title').attr("data-display") == "none") {
			$(this).find('.problme_title').attr("data-display", "block")
			$(this).find('.problme_title').next().find('.ans').css("display", "block")
		}else{
			$(this).find('.problme_title').attr("data-display", "none");
			$(this).find('.problme_title').next().find('.ans').css("display", "none")
		}
		myscroll.refresh();
	})	
}

function pullUpAction(){
	setTimeout(function (){
		m ++;
		if(50*m > Count && 50*(m-1) < Count){
			myProblem(m);
			G_Prg.popMsg('内容已加载完毕');
			$('#pullUp').css('display','none');
		}else if(50*m < Count){
			myProblem(m);
		}
		myscroll.refresh();
    }, 100);
}

function tiaozhuan(){
	$("#list_problem li").on("click", function() {
		var cptID=$(this).attr('cptID');
		var allTestID=$(this).attr('allTestID');
		var page=Number($(this).attr('page'))+1;
		G_Cookie.setCptID(cptID);
		if($('#tab_problem').attr('data-value')==2){
			window.location.href = '/answerExam?fromUrl=searchExam&type=userNote&allTestID='+ allTestID+'&page='+ page+ '&pageType=Recite';
		}
	})	
}
function pullDownAction(){
	setTimeout(function () {
	 	$('#pullDown').css('display','none');
		myscroll.refresh();
    }, 100);
}